package com.mrd.gtimp.business.action;

import com.mrd.constant.MsgConstants;
import com.mrd.exception.QueryException;
import com.mrd.framework.config.web.json.Result;
import com.mrd.framework.config.web.json.annotation.JField;
import com.mrd.framework.config.web.json.annotation.Json;
import com.mrd.framework.ui.Jqgrid;
import com.mrd.framework.ui.filter.SearchRule;
import com.mrd.framework.ui.filter.SearchRuleOption;
import com.mrd.gtimp.business.entity.SuiFuDan;
import com.mrd.gtimp.business.service.SuiFuDanBiz;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

@Controller
@RequestMapping("/sfd")
public class SuiFuDanAction {
	private static final long	serialVersionUID	= 1L;
	@Resource
	private SuiFuDanBiz			suiFuDanBiz;

	private String				jsonString;

	/**
	 * 默认访问入口
	 * @return
	 */
	@RequestMapping("/main")
	public String main(Model model,String tid,String did) {
		model.addAttribute("tid", tid);
		model.addAttribute("did", did);
		return "/gtimp/suifudan/main";
	}

	/**
	 * 默认新增修改入口
	 * @return
	 */
	@RequestMapping("/manage")
	public String manage(Model model,String tid,String did,String id) {
		model.addAttribute("tid", tid);
		model.addAttribute("did", did);
		model.addAttribute("id", id);
		return "/gtimp/suifudan/edit";
	}

	/**
	 * 新增或修改信息
	 * @return
	 */
	@RequestMapping("/saveOrUpdate")
	@ResponseBody
	public Result saveOrUpdate(SuiFuDan suiFuDan) {
		String msg = "";
		// if (checkRight(RoleConstants.SUP_CUSTOMER_SAVE)) {
		try {
			if(com.mrd.util.StringUtils.isEmpty(suiFuDan.getId())) { // 新增操作
				suiFuDan.setId(null);
			}
			suiFuDanBiz.saveOrupdateEntity(suiFuDan);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.newSuccessInstance(MsgConstants.SAVE_FAIL);
		}
		// }
		return Result.newSuccessInstance(MsgConstants.SAVE_SUECCES);
	}

	/**
	 * 编辑信息
	 * @return
	 */
	@RequestMapping("/find")
	@ResponseBody
	public SuiFuDan find(SuiFuDan suiFuDan) {
		if(null != suiFuDan) {
			if(com.mrd.util.StringUtils.isNotEmpty(suiFuDan.getId())) {
				try {
					suiFuDan = suiFuDanBiz.findEntityById(suiFuDan.getId());
				} catch (QueryException e) {
					e.printStackTrace();
				}
			}
		}
		return suiFuDan;
	}

	/**
	 * 删除信息
	 * @return
	 */
	@RequestMapping("/del")
	@ResponseBody
	public Result del(String id) {
		try {
			String[] idArr = id.split(",");
			// StringBuilder sb = new StringBuilder();
			// for(String string : idArr) {
			// sb.append("'").append(string).append("',");
			// }
			List<SuiFuDan> suiFuDanList = new ArrayList<>();
			for(String s : idArr) {
				SuiFuDan suiFuDan = new SuiFuDan();
				suiFuDan.setId(s);
				suiFuDanList.add(suiFuDan);
			}
			if(CollectionUtils.isNotEmpty(suiFuDanList)) {
				suiFuDanBiz.delete(suiFuDanList);
			}
		} catch (Exception e) {
			e.printStackTrace();
			Result.newSuccessInstance(MsgConstants.DEL_FAIL_MSG_ONLY);
		}
		return Result.newSuccessInstance(MsgConstants.DEL_SUCCES_MSG_ONLY);
	}

	/**
	 * 查询
	 * @return
	 */
	@RequestMapping("/findList")
	@ResponseBody
	@Json(value = {@JField("id"), @JField("suifudan_co"), @JField("suifudan_co_cn"),
	        @JField("suifudan_no") })
	public org.springframework.data.domain.Page<SuiFuDan> findList(Model model,String tid,
	        String did,Jqgrid jqgrid) {
		org.springframework.data.domain.Page<SuiFuDan> suiFuDanPage = null;
		if(!StringUtils.isEmpty(tid) || !StringUtils.isEmpty(did)) {
			try {
				SearchRule rule;
				if(!StringUtils.isEmpty(did)) {
					rule = new SearchRule("declaration.id", SearchRuleOption.eq, did);
				} else {
					rule = new SearchRule("temp_id", SearchRuleOption.eq, tid);
				}
				jqgrid.addRule(rule);
				suiFuDanPage = suiFuDanBiz.findByJqgrid(jqgrid);
			} catch (QueryException e) {
				e.printStackTrace();
			}
		}
		return suiFuDanPage;
	}

}
